/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cassandrawindow;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class HandleDataTableBannerHour {
    public static final String KEYSPACE                         =   "AdsData";
    public static final String ColumFamly                       =   "BannerViewCountCf";
    public static final String ColumFamlyBannerClickCountCf     =  "BannerClickCountCf";
    public static final String ColumFamlyBannerViewCountCf      =  "BannerViewCountCf";

    public void handleData() throws ParseException
    {
        long first_time = System.currentTimeMillis();
        CassandraAction cassAct = new CassandraAction(ACCOUNT.CLUSTERNAME ,ACCOUNT.URL, ACCOUNT.PORT);
        cassAct.initReadWirte(KEYSPACE);
        MysqlDataManager mysqldatamanager = new MysqlDataManager();
        MysqlDataManagerMain mysqlMain = new MysqlDataManagerMain();

        CqlDataManager cqldatamanager = new CqlDataManager();
        //List listbanners = cqldatamanager.getAllBannerID();
        List listbanners = mysqlMain.getBannerIDByCPM();
        
        StringBuilder sbff = new StringBuilder();
        Iterator it=listbanners.iterator();
        
        while(it.hasNext())
        {
            String key = (String)it.next();
            String view_hour    = getClickInDay(cassAct, "21-11-2012", key, ColumFamlyBannerViewCountCf);
            String click_hour   = getClickInDay(cassAct, "21-11-2012", key, ColumFamlyBannerClickCountCf);
            
            String str = "("+ key + "," +"'"+ click_hour + "'" + "," + "'" + view_hour + "'" +"," + "'2012-11-21'" +")";

            sbff.append(str).append(",");
            //mysqldatamanager.insertDataBannerHour(key, click_hour, view_hour, "2012-07-03");
            if(sbff.toString().length() > 10000)
            {
                //xu li cho du lieu cuoi
                String relStr = sbff.toString();
                String valInsert = relStr.substring(0, (relStr.length()-1));
                mysqldatamanager.insertDataBannerHour(valInsert);
                sbff.setLength(0);
            }            
            //System.out.println("ket qua  :" +view_hour);
        }

        if(sbff.toString().length() > 1)
        {
            //xu li cho du lieu cuoi
            String relStr = sbff.toString();
            String valInsert = relStr.substring(0, (relStr.length()-1));
            mysqldatamanager.insertDataBannerHour(valInsert);
            sbff.setLength(0);
        }
        long last_time = System.currentTimeMillis();
        System.out.println("Thoi gian chay  :" +(last_time-first_time));
        Debug.println("success HandleDataTableBannerHour");

    }
    
    public static String getClickInDay(CassandraAction cassAct,String day,String key,String columnFamily) throws ParseException
    {

       StringBuilder sb = new StringBuilder();

       for(int i = 0 ; i < 24 ;i++)
       {
           if(i < 10)
           {
                String date = "0"+i+":00:00 "+ day;
                long start_time = TimeUtil.longToDateWithHour(date);
                //them 1h
                long finish_time = start_time + 60*60*1000;
                long countClick = cassAct.getCountClickByIntervalTime(key, columnFamily, start_time, finish_time);
                sb.append(i).append("h:").append(countClick).append(",");
           } else {
                String date = i+":00:00 " + day;
                 long start_time = TimeUtil.longToDateWithHour(date);
                //them 1h
                long finish_time = start_time + 60*60*1000;
                long countClick = cassAct.getCountClickByIntervalTime(key, columnFamily, start_time, finish_time);
                if(i != 23)
                {
                    sb.append(i).append("h:").append(countClick).append(",");
                } else{
                    sb.append(i).append("h:").append(countClick);
                }
           }
       }
       return sb.toString();
    }

    public static String getViewInDay(CassandraAction cassAct,String day,String key,String columnFamily) throws ParseException
    {

       StringBuilder sb = new StringBuilder();

       for(int i = 0 ; i < 24 ;i++)
       {
           if(i < 10)
           {
                String date = "0"+i+":00:00 "+ day;
                long start_time = TimeUtil.longToDateWithHour(date);
                //them 1h
                long finish_time = start_time + 60*60*1000;
                long countClick = cassAct.getCountClickByIntervalTime(key, columnFamily, start_time, finish_time);
                sb.append(i).append("h:").append(countClick).append(",");
           } else {
                String date = i+":00:00 " + day;
                 long start_time = TimeUtil.longToDateWithHour(date);
                //them 1h
                long finish_time = start_time + 60*60*1000;
                long countClick = cassAct.getCountClickByIntervalTime(key, columnFamily, start_time, finish_time);
                if(i != 23)
                {
                    sb.append(i).append("h:").append(countClick).append(",");
                } else{
                    sb.append(i).append("h:").append(countClick);
                }
           }
       }
       return sb.toString();
    }
}
